<?php
class Spider
{
    protected $fans = "https://me.csdn.net/fans/";
    protected $follow = "https://me.csdn.net/follow/";
    protected $info = "https://blog.csdn.net/";

    public function __construct(){

    }

    /**
     * @title 根据用户uid，爬取该用户的粉丝和关注的uid
     * @param string uid 用户唯一id
     * return array 返回爬取到的 用户粉丝和关注的uid组成的数组
     */
    public function crawlList($uid)
    {   
        $list = [];
        $url_arr = array(
            'url_fans' => $this->fans . $uid,
            'url_follow' => $this->follow . $uid
        );
        foreach($url_arr as $key => $url){
            $html = $this->request($url);
            if($html === false)break;
            $html = preg_replace("/\r|\n|\t/","",$html);   // 去掉空白符和换行符，否则匹配不上
            $reg = '#<p class="user_name">[\s]*?<a href="https://me.csdn.net/([^\"]*?)" target="_blank" class="fans">[^<]*?</a>#';
            preg_match_all($reg, $html, $matchs);    // $matchs[0]:全匹配数组， $matchs[1]:子匹配数组
            $list = array_merge($list, $matchs[1]);  // 如果匹配不到数据，$matchs[1]=[]
        }
        return $list;
    }

    /**
     * @title 根据用户uid，爬取该用户的信息
     * @param string uid 用户唯一id
     * return array 返回用户信息
     */
    public function crawlInfo($uid)
    {   
        $info = [];  // 用户信息
        
        // 根据url获取页面
        $info_url = $this->info . $uid;
        $html = $this->request($info_url);

        // if(empty($html)){
        //     return [];
        // }

        // 匹配到了404页面
        $extra_reg = '#<div class="new_404">#';
        $count = preg_match($extra_reg, $html, $rs); 
        if(!empty($count)){ 
            return [];
        }

        // 正则表达式数组
        // 用户名 码龄 原创篇数 粉丝 获赞 评论 访问量 积分 收藏 周排名 总排名 博客等级
        $reg_arr = array(
            'username' => '#<span class="name " username=[\'|\"][^\'\"]*?[\'|\"]>([^<]*?)</span>#',
            'code_age' => '#<span class="personal-home-page">码龄([\d]{1,})年</span>#',
            'raw_count' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<a [^>]*?><span class="count">[^<]*?</span>[\s]*?</a>[\s]*?</dt>[\s]*?<dd><a [^>]*?>原创</a>#',
            'fans_count' => '#<dl class="text-center" id="fanBox" title="([\d]{1,})">#',
            'hot_count' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>获赞</dd>#',
            'comment_count' => '#<dl class="text-center" title="([\d]*?)">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>评论</dd>#',
            'visit_count' => '#<dl class="text-center" style="min-width:58px" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>访问</dd>#',
            'jifen_count' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>积分</dd>#',
            'collect_count' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>收藏</dd>#',
            'week_ranking' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd><a [^>]*?>周排名</a>#',
            'total_ranking' => '#<dl class="text-center" title="([\d]{1,})">[\s]*?<dt>[\s]*?<span class="count">[^<]*?</span>[\s]*?</dt>[\s]*?<dd>[\s]*?<a [^>]*?>总排名</a>#',
            'blog_level'  => '#<dl class="text-center" title="([\d]{1,})级,点击查看等级说明">#'
        );

        $info['uid'] = $uid;
        foreach($reg_arr as $key => $reg){
            preg_match($reg, $html, $res);
            $info[$key] = isset($res[1]) ? trim($res[1]) : "";
            unset($res);
        }

        return $info;
    }

    /**
     * @title 根据url 请求并返回页面
     * return false|string
     */
    public function request($url){
        // 初始化curl
        $curl = curl_init();

        $header = array(
            "Content-type:application/json; text/html; charset=utf-8",
            "Accept:application/json", 
            "Referer:https://me.csdn.net/fans/gshengod", // 伪造成站内请求
            "User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0"
        );

        $opt = array(
            CURLOPT_URL => $url,
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_RETURNTRANSFER => 1,  // 返回数据，不显示页面内
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_SSL_VERIFYHOST => 0,  // 设为 1 的话，没有 ssl安全证书 会报错
            CURLOPT_SSL_VERIFYPEER => 0
        );

        // setopt
        curl_setopt_array($curl, $opt);

        // 发送请求
        $html = curl_exec($curl);

        // 输出错误
        $error = curl_error($curl);

        // 关闭会话
        curl_close($curl);

        return empty($eroor) == true ? $html : $error;
    }
}


$spider = new Spider();

$url = "https://www.shenlanbao.com/p/evaluation_compare?ids=731201223675482112&insType=4";

$html = $spider->request($url);

// $reg = '/<div class="insur-item">([^<]*?)<\/div>/';
$reg = '#<div class="select-head">([^<]*?)</div>#';

preg_match_all($reg, $html, $matchs);

// var_dump($html);

var_dump($matchs);

foreach($matchs as $item){
    foreach($item as $it){
        echo htmlentities($it),PHP_EOL;
    }
    echo "------------------------------------------------------",PHP_EOL;
}